package com.example.composeplayground.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.composeplayground.R
import com.example.composeplayground.common.TokenUtils
import com.example.composeplayground.screens.main.HomeScreen
import com.example.composeplayground.screens.main.LocationScreen
import com.example.composeplayground.screens.main.MeScreen
import com.example.composeplayground.screens.main.OrderScreen

@Preview
@Composable
fun MainScreen(topNavController: NavHostController? = null) {
    val token = TokenUtils.getToken()
    if (token == null) {
        topNavController?.navigate("Login")
    }
    val navController = rememberNavController()
    Scaffold(
        containerColor = Color(0xFFEFEFEF),
        content = { scaffoldPadding ->
            Column(
                modifier = Modifier
                    .padding(scaffoldPadding)
                    .verticalScroll(rememberScrollState())
                    .padding(bottom = 49.dp)
            ) {
                NavHost(navController = navController, startDestination = "Home") {
                    composable("Home") {
                        HomeScreen()
                    }
                    composable("Location") {
                        LocationScreen()
                    }
                    composable("Me") {
                        MeScreen()
                    }
                    composable("Order") {
                        OrderScreen()
                    }
                }
            }
        },
        bottomBar = {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = Color(0xFFFFFFFF))
                    .padding(top = 11.dp, start = 11.dp, end = 11.dp, bottom = 0.dp)
            ) {
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    data class BottomBarItem(val onImg: Int, val offImg: Int, val url: String)
                    listOf(
                        listOf(
                            BottomBarItem(
                                R.mipmap.icon_home_selected,
                                R.mipmap.icon_home_unselected,
                                "Home"
                            ),
                            BottomBarItem(
                                R.mipmap.icon_charge_selected,
                                R.mipmap.icon_charge_unselected,
                                "Location"
                            ),
                        ),
                        listOf(
                            BottomBarItem(
                                R.mipmap.icon_activities_selected,
                                R.mipmap.icon_activities_unselected,
                                "Order"
                            ),
                            BottomBarItem(
                                R.mipmap.icon_account_selected,
                                R.mipmap.icon_account_unselected,
                                "Me"
                            ),
                        )
                    ).forEach {
                        Row(horizontalArrangement = Arrangement.spacedBy(0.dp)) {
                            it.forEach { bottomBarItem ->
                                Box(
                                    modifier = Modifier
                                        .width(75.dp)
                                        .height(44.dp)
                                        .clickable {
                                            if (navController.currentDestination?.route != bottomBarItem.url) {
                                                navController.navigate(bottomBarItem.url)
                                            } else {
                                                println("相同的url")
                                            }
                                        }
                                ) {
                                    println(navController.currentDestination?.route)
                                    val navBackStackEntry by navController.currentBackStackEntryAsState()
                                    val currentDestination = navBackStackEntry?.destination
                                    val selected =
                                        currentDestination?.hierarchy?.any { it.route == bottomBarItem.url } == true
                                    Image(
                                        painter = painterResource(id = if (selected) bottomBarItem.onImg else bottomBarItem.offImg),
                                        contentDescription = null,
                                        modifier = Modifier
                                            .fillMaxSize(),
                                        contentScale = ContentScale.FillBounds
                                    )
                                }
                            }
                        }
                    }
                }
                Box(
                    modifier = Modifier
                        .offset(y = (-20).dp)
                        .shadow(
                            elevation = 10.dp,
                            shape = CircleShape
                        )
                        .clip(
                            shape = CircleShape
                        )
                        .background(color = Color(0xFF339486))
                        .size(75.dp)
                        .padding(25.dp)
                        .align(Alignment.TopCenter)
                ) {
                    Image(
                        painter = painterResource(id = R.mipmap.icon_home_qrcode),
                        contentDescription = null,
                        modifier = Modifier
                            .fillMaxSize(),
                        contentScale = ContentScale.FillBounds
                    )
                }
            }
        })
}